

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="">
  <meta name="author" content="John Doe">
  <meta name="keywords" content="">
  
  <title>CFR715(Div.2)2021.04.16 - violet apricity</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.4.0/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"example.com","root":"/","version":"1.8.9","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}}};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>violet apricity</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" href="javascript:">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="CFR715(Div.2)2021.04.16">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-04-17 14:51" pubdate>
        2021年4月17日 下午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      1.9k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      27
       分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">CFR715(Div.2)2021.04.16</h1>
            
            <div class="markdown-body">
              <h1 id="Codeforces-Round-715-Div-2-2021-04-16"><a href="#Codeforces-Round-715-Div-2-2021-04-16" class="headerlink" title="Codeforces Round #715 (Div. 2)2021.04.16"></a><a target="_blank" rel="noopener" href="https://codeforces.com/contest/1509">Codeforces Round #715 (Div. 2)2021.04.16</a></h1><p>闲话：这场直接迟到了半小时，不过前两道题切的很快，半小时直接两题，挺舒服。不过c很可惜。我读完题觉得是区间dp，隐约记得寒假见过一道区间dp的题，但是时间不够加上不熟练，没写出来。赛后看了一眼群里讨论发现几乎就板子题，真是太可惜了。不过没关系，借此再熟悉一手（一点点）区间dp。</p>
<h1 id="A-Average-Height"><a href="#A-Average-Height" class="headerlink" title="A. Average Height"></a><a target="_blank" rel="noopener" href="https://codeforces.com/contest/1509/problem/A">A. Average Height</a></h1><h2 id="题意："><a href="#题意：" class="headerlink" title="题意："></a>题意：</h2><p>t组样例，每组给一个n和长为n的序列，要求对其重新排列，使得满足<strong>相邻两项和除以2为整数</strong>这样的数的对数最大化。</p>
<h2 id="题解："><a href="#题解：" class="headerlink" title="题解："></a>题解：</h2><p>要想满足两项之和除以2为整数，也就是和为偶数，那么它们的奇偶性应该相同，于是就很简单了，把奇数排在一起，偶数排在一起。<br>太签到了，读完题就切。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br></pre></td><td class="code"><pre><code class="hljs javascript">        <span class="hljs-comment">// violet apricity</span><br><span class="hljs-comment">// Do all I can do.Do good to be good.</span><br><br>#include&lt;iostream&gt;<br>#include&lt;stdio.h&gt;<br>#include&lt;cstring&gt;<br>#include&lt;string&gt;<br>#include&lt;algorithm&gt;<br>#include&lt;vector&gt;<br>#include&lt;math.h&gt;<br>#include&lt;map&gt;<br>#include&lt;sstream&gt;<br><br>#define STD using namespace std;<br>#define ll long long<br>#define db double<br>#define ldb long double<br>#define IOS std::ios::sync_with_stdio(<span class="hljs-literal">false</span>),<span class="hljs-attr">std</span>::cin.tie(<span class="hljs-number">0</span>),<span class="hljs-attr">std</span>::cout.tie(<span class="hljs-number">0</span>);<br>#define MAX <span class="hljs-number">88888888</span><br>#define INF <span class="hljs-number">0x3f</span><br>#define r0 <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>#define SYP system(<span class="hljs-string">&quot;pause&quot;</span>);<br>#define endl <span class="hljs-string">&#x27;\n&#x27;</span><br>int main()<br>&#123;<br>    IOS<br>    int t;std::cin&gt;&gt;t;<br>    <span class="hljs-keyword">while</span>(t--)&#123;<br>        int n;std::cin&gt;&gt;n;<br>        std::vector&lt;int&gt;x,y;<br>        <span class="hljs-keyword">while</span>(n--)&#123;<br>            int d;std::cin&gt;&gt;d;<br>            <span class="hljs-keyword">if</span>(d%<span class="hljs-number">2</span>==<span class="hljs-number">1</span>)x.push_back(d);<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(d%<span class="hljs-number">2</span>==<span class="hljs-number">0</span>)y.push_back(d);<br>        &#125;<br>        <span class="hljs-keyword">for</span>(auto i:x)std::cout&lt;&lt;i&lt;&lt;<span class="hljs-string">&#x27; &#x27;</span>;<br>        <span class="hljs-keyword">for</span>(auto i:y)std::cout&lt;&lt;i&lt;&lt;<span class="hljs-string">&#x27; &#x27;</span>;<br>        std::cout&lt;&lt;<span class="hljs-string">&#x27;\n&#x27;</span>;<br>    &#125;<br>    <span class="hljs-comment">//SYP</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="B-TMT-Document"><a href="#B-TMT-Document" class="headerlink" title="B. TMT Document"></a><a target="_blank" rel="noopener" href="https://codeforces.com/contest/1509/problem/B">B. TMT Document</a></h1><h2 id="题意：-1"><a href="#题意：-1" class="headerlink" title="题意："></a>题意：</h2><p>t组样例，每组给一个n和长为n且只包含‘T’和‘M’的字符串s。将s划分为若干个不相交的子序列且包含该字符串所有的字符，判断是否有某种方案，使每个子序列均为TMT。</p>
<h2 id="题解：-1"><a href="#题解：-1" class="headerlink" title="题解："></a>题解：</h2><p>字符串匹配问题（我觉得是），这里子序列可以跨着分，但是对于每个子序列必须是TMT，那么必须是中间一个M两边各一个T，稍微想一下就发现了，对于每一个M，它的前面和后面都必须有个T和它匹配。那么我们可以从头遍历s，记录对应字符的数量，如果T的数量小于M的数量，那么说明有某个M前面没有T来和它匹配。同样的，再从尾遍历判断每个M后面是否有T和它匹配。<br>最后因为每个字符都要用到，所以数量上应该满足T=2*M。（题目说了n是3的倍数，所以这一点不用判断）<br>依旧是没什么算法的简单思维题。</p>
<figure class="highlight javascript"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><code class="hljs javascript">        <span class="hljs-comment">// violet apricity</span><br><span class="hljs-comment">// Do all I can do.Do good to be good.</span><br><br>#include&lt;iostream&gt;<br>#include&lt;stdio.h&gt;<br>#include&lt;cstring&gt;<br>#include&lt;string&gt;<br>#include&lt;algorithm&gt;<br>#include&lt;vector&gt;<br>#include&lt;math.h&gt;<br>#include&lt;map&gt;<br>#include&lt;sstream&gt;<br><br>#define STD using namespace std;<br>#define ll long long<br>#define db double<br>#define ldb long double<br>#define IOS std::ios::sync_with_stdio(<span class="hljs-literal">false</span>),<span class="hljs-attr">std</span>::cin.tie(<span class="hljs-number">0</span>),<span class="hljs-attr">std</span>::cout.tie(<span class="hljs-number">0</span>);<br>#define MAX <span class="hljs-number">88888888</span><br>#define INF <span class="hljs-number">0x3f</span><br>#define r0 <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>#define SYP system(<span class="hljs-string">&quot;pause&quot;</span>);<br>#define endl <span class="hljs-string">&#x27;\n&#x27;</span><br>STD<br>int main()<br>&#123;<br>    IOS<br>    string s;<br>    int t;cin&gt;&gt;t;<br>    <span class="hljs-keyword">while</span>(t--)&#123;<br>        int len;<br>        cin&gt;&gt;len;<br>        cin&gt;&gt;s;<br>        int m=<span class="hljs-number">0</span>,t=<span class="hljs-number">0</span>;<br>        bool flag=<span class="hljs-number">1</span>;<br>        <span class="hljs-keyword">for</span>(int i=<span class="hljs-number">0</span>;i&lt;len;i++)&#123;<br>            <span class="hljs-keyword">if</span>(s[i]==<span class="hljs-string">&#x27;T&#x27;</span>)t++;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(s[i]==<span class="hljs-string">&#x27;M&#x27;</span>)m++;<br>            <span class="hljs-keyword">if</span>(m&gt;t)&#123;flag=<span class="hljs-number">0</span>;<span class="hljs-keyword">break</span>;&#125;<br>        &#125;<br>        int mm=<span class="hljs-number">0</span>,tt=<span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">for</span>(int i=len-<span class="hljs-number">1</span>;i&gt;=<span class="hljs-number">0</span>;i--)&#123;<br>            <span class="hljs-keyword">if</span>(s[i]==<span class="hljs-string">&#x27;T&#x27;</span>)tt++;<br>            <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span>(s[i]==<span class="hljs-string">&#x27;M&#x27;</span>)mm++;<br>            <span class="hljs-keyword">if</span>(mm&gt;tt)&#123;flag=<span class="hljs-number">0</span>;<span class="hljs-keyword">break</span>;&#125;<br>        &#125;<br>        <span class="hljs-keyword">if</span>(t!=<span class="hljs-number">2</span>*m||tt!=<span class="hljs-number">2</span>*mm)flag=<span class="hljs-number">0</span>;<br>        <span class="hljs-keyword">if</span>(flag==<span class="hljs-number">0</span>)cout&lt;&lt;<span class="hljs-string">&quot;NO\n&quot;</span>;<br>        <span class="hljs-keyword">else</span> cout&lt;&lt;<span class="hljs-string">&quot;YES\n&quot;</span>;<br>    &#125;<br>   <span class="hljs-comment">// SYP</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<h1 id="C-The-Sports-Festival"><a href="#C-The-Sports-Festival" class="headerlink" title="C. The Sports Festival"></a><a target="_blank" rel="noopener" href="https://codeforces.com/contest/1509/problem/C">C. The Sports Festival</a></h1><h2 id="题意：-2"><a href="#题意：-2" class="headerlink" title="题意："></a>题意：</h2><p>给出一个n和长为n的序列a，对其重排列，使d[1]+d[2]+~d[n]最小化。<br>其中d[i]=max(a[1],a[2], ~, a[i])-min(a[1],a[2], ~ a[i])。</p>
<h2 id="题解：-2"><a href="#题解：-2" class="headerlink" title="题解："></a>题解：</h2><p>一道区间dp题。<br>首先要把序列从小到大sort一下。<br>假设现在有：a[1]&lt;=a[2]&lt;=a[3]&lt;=a[4]&lt;=a[5]，n=5；<br>首先我们来解释一下方案的选择：我们先选第一项，假设现在选a[3]，则max=min，d[1]=0；<br>再来选第二项和第三项，我们来对比一下选a[4],a[5]和选a[5],a[4]的情况：<br>1.d[2]+d[3]=(a[4]-a[3])+(a[5]-a[3])=a[3]+a[4]-2* a[3]<br>2.d[2]+d[3]=(a[5]-a[3])+(a[5]-a[3])=2* a[5]-2* a[3]<br>很明显上面是要比下面的小的，所以我们选择的时候依次往旁边推，这样子比跨过一个数要小。<br>更普遍的解释是：要使max-min小，即让max和min尽量相近，那么我们可以在中间选区间然后一次往两边判断加上左边的数还是右边的数作为新区间，且要让最大的和最小的出现在序列的极端，具体实现我们可以dp一下。<br>状态转移方程为：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs cpp">dp[l][r]=a[r]-a[l]+<span class="hljs-built_in">min</span>(dp[l+<span class="hljs-number">1</span>][r],dp[l][r+<span class="hljs-number">1</span>]);<br><span class="hljs-comment">//区间[l,r]下的d和最小值</span><br></code></pre></td></tr></table></figure>

<p>我们知道区间dp是把两个区间的信息整合成一个大区间的信息，这里每次只增加一个数，所以是一个区间左边加上一个包含一个数的区间或者右边加上一个包含一个数的区间。<br>假设我们现在已经选择了一个处于中间的区间[l,r]，该区间贡献值为x（小的那个区间），新区间贡献为y（大的那个区间），有以下两种方案：<br>选左边小的：y=x+a[r]-a[l-1]；<br>选右边大的：y=x+a[r+1]-a[l]；<br>比较以下哪种方案小就选择它。然后更新一下继续往下选。<br>上述是大往小递推解释，实现的时候为了方便我们从从小往大的方向记忆化枚举，下面在代码中解释：</p>
<figure class="highlight cpp"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br></pre></td><td class="code"><pre><code class="hljs cpp">        <span class="hljs-comment">// violet apricity</span><br><span class="hljs-comment">// Do all I can do.Do good to be good.</span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;iostream&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;stdio.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;cstring&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;string&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;algorithm&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;vector&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;math.h&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;map&gt;</span></span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">include</span><span class="hljs-meta-string">&lt;sstream&gt;</span></span><br><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> STD using namespace std;</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> ll long long</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> db double</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> ldb long double</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> IOS std::ios::sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> MAX 88888888</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> INF 0x3f</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> r0 return 0;</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> SYP system(<span class="hljs-meta-string">&quot;pause&quot;</span>);</span><br><span class="hljs-meta">#<span class="hljs-meta-keyword">define</span> endl <span class="hljs-meta-string">&#x27;\n&#x27;</span></span><br><br><span class="hljs-keyword">const</span> <span class="hljs-keyword">int</span> N=<span class="hljs-number">2021</span>;<br>ll a[N],dp[N][N];<br>STD<br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">main</span><span class="hljs-params">()</span></span><br><span class="hljs-function"></span>&#123;<br>    <span class="hljs-comment">//IOS</span><br>    ll n;cin&gt;&gt;n;<br>    <span class="hljs-keyword">for</span>(ll i=<span class="hljs-number">1</span>;i&lt;=n;i++)cin&gt;&gt;a[i];<br>    <span class="hljs-built_in">sort</span>(a+<span class="hljs-number">1</span>,a+n+<span class="hljs-number">1</span>);<br>    <span class="hljs-keyword">for</span>(ll i=<span class="hljs-number">2</span>;i&lt;=n;i++)<span class="hljs-comment">//这里是枚举当前区间的长度，因为长为1的区间只有一个数字，它的贡献为0，所以可以不用枚举</span><br>        <span class="hljs-keyword">for</span>(ll j=<span class="hljs-number">1</span>;j+i<span class="hljs-number">-1</span>&lt;=n;j++)&#123;<span class="hljs-comment">//这里是枚举所有长为i的区间，下面内容对每个区间记忆化dp</span><br>            ll l=j,r=j+i<span class="hljs-number">-1</span>;<span class="hljs-comment">//这样子写方便理解，分别为左右端点</span><br>            dp[l][r]=<span class="hljs-built_in">min</span>(dp[l][r<span class="hljs-number">-1</span>],dp[l+<span class="hljs-number">1</span>][r])+a[r]-a[l];<br>            <span class="hljs-comment">//当前情况转移来的原来两种情况取最小 加上当前情况多出来的贡献</span><br>    &#125;<br>    cout&lt;&lt;dp[<span class="hljs-number">1</span>][n]&lt;&lt;<span class="hljs-string">&#x27;\n&#x27;</span>;<span class="hljs-comment">//最后答案为区间[1,n]的贡献</span><br>    <span class="hljs-comment">// SYP</span><br>    <span class="hljs-keyword">return</span> <span class="hljs-number">0</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>好了依旧是ABC场，没看的就没必要写了，下次争取切3读4。</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/ACM/">ACM</a>
                    
                      <a class="hover-with-bg" href="/tags/codeforces/">codeforces</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/04/18/PackDP/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">背包dp问题</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/04/16/GZHU2021week7/">
                        <span class="hidden-mobile">广州大学ACM2021第七周训练2021.04.13</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.5.3/dist/js/bootstrap.min.js" ></script>
<script  src="/js/debouncer.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.0/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.0/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.6/dist/clipboard.min.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.11/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>



  <script  src="/js/local-search.js" ></script>
  <script>
    (function () {
      var path = "/local-search.xml";
      $('#local-search-input').on('click', function() {
        searchFunc(path, 'local-search-input', 'local-search-result');
      });
      $('#modalSearch').on('shown.bs.modal', function() {
        $('#local-search-input').focus();
      });
    })()
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
